<script setup>
defineProps({ sessions: Array, selectedId: Number })
defineEmits(['create', 'select', 'delete'])

function formatTime(ts) {
  if (!ts) return ''
  let d
  if (typeof ts === 'number') {
    d = new Date(ts * 1000)
  } else if (typeof ts === 'string') {
    const num = Number(ts)
    if (!Number.isNaN(num)) {
      d = new Date(num * 1000)
    } else {
      d = new Date(ts)
    }
  } else {
    d = new Date(ts)
  }
  if (isNaN(d.getTime())) return ''
  const y = d.getFullYear()
  const m = String(d.getMonth() + 1).padStart(2, '0')
  const day = String(d.getDate()).padStart(2, '0')
  return `${y}/${m}/${day}`
}
</script>

<template>
  <div class="chat-sidebar">
    <div class="sidebar-header">
      <h3>聊天会话</h3>
      <div class="header-actions">
        <button class="new-chat-btn" @click="$emit('create')" title="新建会话">
          <svg width="16" height="16" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
            <path d="M12 5v14M5 12h14" stroke="currentColor" stroke-width="2" stroke-linecap="round"
              stroke-linejoin="round" />
          </svg>
        </button>
      </div>
    </div>
    <div class="history-list">
      <div v-if="!sessions || sessions.length === 0" class="no-history">
        <p>暂无聊天会话</p>
      </div>
      <div v-for="s in sessions" :key="s.sessionId" class="history-item" :class="{ active: selectedId === s.sessionId }"
        @click="$emit('select', s)">
        <div class="history-content">
          <div class="history-question">{{ s.sessionTitle }}</div>
          <div class="session-info">
            <span class="history-time">{{ formatTime(s.lastActiveTime) }}</span>
          </div>
          <button class="floating-delete-btn" :disabled="!selectedId" title="删除当前会话"
            @click.stop="$emit('delete', selectedId)">
            <svg width="16" height="16" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
              <path d="M3 6h18M8 6V4a2 2 0 012-2h4a2 2 0 012 2v2m3 0v14a2 2 0 01-2 2H7a2 2 0 01-2-2V6h14z"
                stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" />
            </svg>
          </button>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.chat-sidebar {
  border-right: 1px solid #e0e0e0;
  padding: 12px;
  overflow: auto;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}

.sidebar-header h3 {
  margin: 0;
  font-size: 14px;
  color: #000;
}

.new-chat-btn {
  background: #000;
  color: #fff;
  border: none;
  width: 28px;
  height: 28px;
  border-radius: 6px;
  cursor: pointer;
  display: grid;
  place-items: center;
}

.history-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.no-history {
  color: #777;
  font-size: 13px;
}

.history-item {
  background: #ffffff;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  padding: 10px;
  cursor: pointer;
}

.history-item.active {
  border-color: #bbbbbb;
  background: #f0f0f0;
}

.history-content {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  align-items: center;
}

.history-question {
  color: #121212;
  font-size: 14px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.session-info {
  color: #777;
  font-size: 12px;
}

.floating-delete-btn {
  background: transparent;
  border: 1px solid #d0d0d0;
  color: #555;
  width: 28px;
  height: 28px;
  border-radius: 6px;
  cursor: pointer;
}

.floating-delete-btn:disabled {
  opacity: .5;
  cursor: not-allowed;
}
</style>
